<?php
require_once('converters/converters.php');
/**
 * ConvertHelper
 *
 * @package default
 * @author Keith
 **/
class ConvertHelper
{
	const FACET_XML_SAMPLE = '<facet-results';
	
	const SPARQL_XML_SAMPLE = '<sparql';
	
	
	
			
	function choose_input($url, $response_object)
	{
		 $recognised_mimetypes = array(

			'application/rdf+xml' => 'rdf-xml',
			'application/rdf+json' => 'rdf-json',
			'application/x-rdf+json' => 'rdf-json',
			'application/sparql-results+xml' => 'sparql-xml',
			'application/sparql-results+json' => 'sparql-json',
			'application/xhtml+xml' => 'rdf-html',
			'text/html' => 'rdf-html',
			'application/turtle' => 'rdf-turtle',
			'application/x-turtle' => 'rdf-turtle',
			'text/x-turtle' => 'rdf-turtle',
			'text/turtle' => 'rdf-turtle',
			'text/rdf+n3' => 'rdf-turtle',
			);
		//Check Known Specific Mime-types
		if($content_type = trim(array_shift(explode( ';', trim($response_object->headers['content-type'])))))
		{
			foreach($recognised_mimetypes as $mime => $format)
			{
				if(strcasecmp( $content_type, $mime)===0) return $format;
			}
		}

		$content = trim(preg_replace("/<\?xml(.*?)\?>/", '', $response_object->body));
		
		//Check non-specific XML mimetypes
		if(
			!isset($response_object->headers['content-type']) OR 
			$content_type=='application/xml' OR
			$content_type=='text/xml'
			
			// Checking that, if content-type is set, it contains xml
		)
		{
			if( substr($content, 0, strlen(self::FACET_XML_SAMPLE))==self::FACET_XML_SAMPLE ) //facet-xml
			{
				return 'facet-xml';
			}
			elseif(substr($content, 0, strlen(self::SPARQL_XML_SAMPLE))==self::SPARQL_XML_SAMPLE) // SPARQL_XML
			{
				return 'sparql-xml';
			}
			else if(preg_match('/<[a-z0-9A-Z]+:RDF/', substr($content,0,20))) // rdf-xml
			{
				return 'rdf-xml';
			} 
		}
		else if(in_array($content_type, array('text/plain','application/json')))
		{
			//is it json?
			$json = json_decode($content, true);
			if(is_array($json))
			{
				require_once 'converters/GraphIndex.class.php';
				require_once 'converters/ExhibitArray.class.php';
				require_once 'converters/FacetArray.class.php';
				require_once 'converters/SPARQL_Array.class.php';
				
				if(GraphIndex::is_valid($json))
				{
					return 'rdf-json';
				}
				else if(ExhibitArray::is_valid($json))
				{
					return 'exhibit-json';
				}
				else if(FacetArray::is_valid($json))
				{
					return 'facet-json';
				}
				else if(SPARQL_Array::is_valid($json))
				{
					return 'sparql-json';
				}
				
			}
			elseif($content_type=='application/json')
			{
				return 'invalid-json';
			} 
			else 
			{
				return 'rdf-turtle';
			}
		}
	}
	
	function accept_header_to_list($header)
	{
		$mimes = explode(',',$header);
		$accept_mimetypes = array();
		foreach($mimes as $mime)
		{
			$parts = explode(';q=', $mime);
			if(count($parts)>1)
			{
				$accept_mimetypes[$parts[0]]=$parts[1];
			}
			else
			{
				$accept_mimetypes[$mime]=1;
			}
		}

		arsort($accept_mimetypes);
		return array_keys($accept_mimetypes);

	}

	function get_filename_extension($inputType, $output_param=false, $accept_mimes=array())
	{

		$output_exts = array(
				'exhibit-json' =>'json',
				'exhibit-jsonp' => 'jsonp'
			);
					
		$rdf_conversions = array(
				'rdf',
				'rss',
				'turtle',
				'html',
				'json',
				'jsonp',
				'xml',
				'exhibit',
				'trix',
				'phps',
				'edit',
				'ntriples',

			);
		
		$standard_conversions = array('html','json','jsonp', 'xml','phps');
		$sparql_xml_conversions = array('html','srj','json','jsonp', 'xml','phps');
		$sparql_json_conversions = array('html','srj','jsonp','phps');
		$json_conversions = array('html','json','jsonp','phps');
		$conversions = array(
				'html' => $rdf_conversions,
				'rdf' => $rdf_conversions,
				'rdf-xml' => $rdf_conversions,
				'rdf-html' => $rdf_conversions,
				'rdf-turtle' => $rdf_conversions,
				'rdf-json' => $rdf_conversions,
				'facet-xml' => $standard_conversions,
				'sparql-xml' => $sparql_xml_conversions,
				'sparql-json' => $sparql_json_conversions,
				'facet-json' => $json_conversions,
			);
		
		
		
		//calculate conversion and fileformat
		if($output_param)
		{
			if(
			isset($output_exts[$output_param]) AND 
			isset($conversions[$inputType]) AND 
			in_array($output_exts[$output_param], $conversions[$inputType])
			)
			{
				return $output_exts[$output_param];
			}
			elseif(
					isset($conversions[$inputType]) AND
					in_array($output_param, $conversions[$inputType])
				)
			{
				return $output_param;
			}
			else
			{
				return false;
			}
		}
		else if(!empty($accept_mimes))
		{
			$dir = is_dir('templates')? 'templates' : '../templates';
			$templates = scandir($dir);
			$recognised_extensions =  array_filter(array_map(create_function('$a','return (strstr($a,"default."))? substr($a,8) : false;'), $templates));

			array_unshift($recognised_extensions, 'html');
			require 'config/mimetypes.php';

			
			foreach($accept_mimes as $mime)
			{

				foreach($mimetypes as $ext => $server_mime)
				{
					if($server_mime==$mime AND 
						in_array($ext, $recognised_extensions) AND
						in_array($ext, $conversions[$inputType])
						)
						{
							return $ext;
						} 
				}
				
			}

			return false;
		}
		else
		{
			return false;
		}
		
	}

	
	
} // END class ConvertHelper


?>